local zhenglie = fk.CreateSkill{
  name = "xiaobai__zhenglie",
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["xiaobai__zhenglie"] = "铮烈",
  [":xiaobai__zhenglie"] = "每回合限一次，你可以将三张手牌当【无懈可击】或【决斗】使用，"..
    "此牌结算后，你可以使用任意张红色底牌，若该转化牌未被其他角色响应，则你使用的底牌不能被响应。",
  ["#xiaobai__zhenglie"] = "铮烈：你可以将三张手牌当【无懈可击】或【决斗】使用"
}

zhenglie:addEffect("viewas", {
  pattern = "nullification,duel",
  prompt = "#xiaobai__zhenglie",
  card_filter = function (self, player, to_select, selected)
    return #selected < 3 and table.contains(player:getCardIds("h"), to_select)
  end,
  view_as = function (self, player, cards)
    if #cards < 3 then return nil end
    local card 
    if Fk.currentResponsePattern then
      card = Fk:cloneCard("nullification")
    else
      card = Fk:cloneCard("duel")
    end
    card:addSubcards(cards)
    card.skillName = zhenglie.name
    return card
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(zhenglie.name, Player.HistoryTurn) < 1 and player:getHandcardNum() >= 3
  end,
  enabled_at_response = function (self, player, response)
    return (not response) and player:usedSkillTimes(zhenglie.name, Player.HistoryTurn) < 1 and player:getHandcardNum() >= 3
  end,
  after_use = function (self, player, use)
    local cards = table.filter(use.card.subcards, function (cid)
      return Fk:getCardById(cid).color == Card.Red
    end)
    local room = player.room
    while #cards > 0 do
      local use_card = room:askToUseRealCard(player, {
        expand_pile = cards,
        skip = true,
        pattern = cards,
        skill_name = zhenglie.name,
      })
      if not use_card then return end
      use_card.disresponsiveList = use_card.disresponsiveList or {}
      if use.cardsResponded == nil or #use.cardsResponded == 0 then
        table.insertTableIfNeed(use_card.disresponsiveList, room.alive_players)
      end
      room:useCard(use_card)
      table.removeOne(cards, use_card.card.id)
    end
  end
})

return zhenglie